﻿using System;
using System.Data;
using System.Linq;
using System.Web.Mvc;
using PMSMVC.Models;
using Webdiyer.WebControls.Mvc;

namespace PMSMVC.Controllers
{
    [Authorize]
    public class DocTaskController : Controller
    {
        private PMSDBContext db = new PMSDBContext();


        #region DocTask列表用
        //
        // GET: /DocTask/
        public ActionResult Index(string projectID, int? id)
        {
            ViewBag.ProjectID = projectID;
            var doctasks = from d in db.DocTasks
                           where d.ProjectID == projectID
                           select d;
            PagedList<DocTask> list = doctasks.OrderBy(p => p.PostTime).ToPagedList(id ?? 1, 10);
            if (Request.IsAjaxRequest())
                return PartialView("DocTasklist", list);
            return View(list);
        }

        public ActionResult MyIndex(int? id)
        {
            var doctasks1 = from d in db.DocTasks
                           where d.TaskStatus == "OPEN" && d.Executor == User.Identity.Name
                           select d;
            var doctasks2 = from d in db.DocTasks
                           where d.TaskStatus == "OPEN" && d.Checker == User.Identity.Name
                           select d;
            PagedList<DocTask> list1 = doctasks1.OrderBy(p => p.PostTime).ToPagedList(id ?? 1, 10);
            PagedList<DocTask> list2 = doctasks2.OrderBy(p => p.PostTime).ToPagedList(id ?? 1, 10);
            PagedList<DocTask>[] list = new PagedList<DocTask>[2] { list1, list2 };
            if (Request.IsAjaxRequest())
                return PartialView("DocTasklist", list);
            return View(list);
        }

        public ActionResult SearchIndex(string SProjectID, string SProjectName, string SProductTypeID, string SCustomerID, string SPublisher, string SExecutor, int? id)
        {
            var doctasks = from d in db.DocTasks
                           select d;
            doctasks = doctasks.Where(c => (c.ProjectID.Contains(SProjectID) || c.ProjectID == null)
                && (c.Project.ProjectName.Contains(SProjectName) || c.Project.ProjectName == null)
                && (c.Publisher.Contains(SPublisher) || c.Publisher == null)
                && (c.Executor.Contains(SExecutor) || c.Executor == null));

            if (!string.IsNullOrEmpty(SProductTypeID))
            {
                doctasks = from d in doctasks
                           from p in db.ProductTypes
                           where d.ProjectID == p.ProjectID && p.ProductTypeID.Contains(SProductTypeID)
                           select d;
            }
            if (!string.IsNullOrEmpty(SCustomerID))
            {
                doctasks = from d in doctasks
                           from p in db.ProductTypes
                           where d.ProjectID == p.ProjectID && p.CustomerID.Contains(SCustomerID)
                           select d;
            }
            PagedList<DocTask> list = doctasks.OrderBy(p => p.PostTime).ToPagedList(id ?? 1, 10);
            if (Request.IsAjaxRequest())
                return PartialView("DocTasklist", list);
            return View(list);
        }
        #endregion

        //
        // GET: /DocTask/Details/5

        public ViewResult Details(int id)
        {
            DocTask doctask = db.DocTasks.Find(id);
            return View(doctask);
        }

        //
        // GET: /DocTask/Create

        public ActionResult Create(string projectID)
        {
            ViewBag.ProjectID = projectID;            
            return View();
        } 

        //
        // POST: /DocTask/Create

        [HttpPost]
        public ActionResult Create(DocTask doctask)
        {
            if (ModelState.IsValid)
            {
                doctask.RecordTime = DateTime.Now;
                doctask.Publisher = User.Identity.Name;
                doctask.TaskStatus = "OPEN";
                db.Entry(doctask).State = EntityState.Added;
                //db.DocTasks.Add(doctask);
                db.SaveChanges();
                return RedirectToAction("Index", new { projectID = doctask.ProjectID });  
            }
            return View(doctask);
        }
        
        //
        // GET: /DocTask/Edit/5
 
        public ActionResult Edit(int id)
        {
            //DocTask doctask = db.DocTasks.Find(id);
            //return View(doctask);
            DocTask doctask = db.DocTasks.Find(id);
            DocTaskEditModel doctaskeditmodel = new DocTaskEditModel{DocTask=doctask,RecordTime= DateTime.MinValue,UserID= null,ChangeDetail= null};
            return View(doctaskeditmodel);
        }

        //
        // POST: /DocTask/Edit/5

        [HttpPost]
        public ActionResult Edit(DocTaskEditModel doctaskeditmodel)
        {
            if (ModelState.IsValid)
            {
                DocTask doctask = doctaskeditmodel.DocTask;
                DocTaskChange doctaskchange = new DocTaskChange
                {
                    TaskID = doctaskeditmodel.DocTask.TaskID,
                    RecordTime = DateTime.Now,
                    UserID = User.Identity.Name,
                    ChangeDetail = doctaskeditmodel.ChangeDetail
                };
                db.Entry(doctask).State = EntityState.Modified;
                db.Entry(doctaskchange).State = EntityState.Added;
                db.SaveChanges();
                return RedirectToAction("Index", new { projectID = doctask.ProjectID });
            }
            return View(doctaskeditmodel);
        }

        //
        // GET: /DocTask/Delete/5
 
        public ActionResult Delete(int id)
        {
            DocTask doctask = db.DocTasks.Find(id);
            return View(doctask);
        }

        //
        // POST: /DocTask/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            DocTask doctask = db.DocTasks.Find(id);
            string ProjectID = doctask.ProjectID;
            db.Entry(doctask).State = EntityState.Deleted;
            //db.DocTasks.Remove(doctask);
            db.SaveChanges();
            return RedirectToAction("Index", new { projectID = ProjectID });
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}